package cn.mark.test.serlvet;

import java.io.IOException;

import javax.servlet.AsyncContext;
import javax.servlet.ReadListener;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns="/uploadIOFile/*",asyncSupported=true)
@MultipartConfig(location="/home/mark/temp/file" )
public class UploadIOServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		resp.getWriter().println("Upload start");
		super.service(req, resp);
//		
//		System.out.println("getContextPath" + req.getContextPath());
//		System.out.println("getServletPath" + req.getServletPath());
//		System.out.println("getPathInfo" + req.getPathInfo());
//		
//		HttpSession session = req.getSession();
//		
//		System.out.println("Session ID: " + session.getId());
//		
//		
//		resp.getWriter().println("req.getRequestURI(): " + req.getRequestURI());
//		resp.getWriter().println("resp.encodeURL(req.getRequestURI()): " + resp.encodeURL(req.getRequestURI()));
////		resp.encodeURL(req.getRequestURI()); // 会将sessionId保持在URL,如果client设置cookie允许，则保持不变
//		
//		
//	
//		
//		
//		resp.getWriter().println("World2");
//		
////		req.getRequestDispatcher("/report2").include(req, resp);
////		req.getRequestDispatcher("/report2").forward(req, resp);
//		
//		resp.getWriter().println("getRequestDispatcher end");
		
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		final AsyncContext acontext = req.startAsync();
	      final ServletInputStream input = req.getInputStream();
	      
	      System.out.println("input.isRead: " + input.isReady());

		 input.setReadListener(new ReadListener() {
	         byte buffer[] = new byte[4*1024];
	         StringBuilder sbuilder = new StringBuilder();
	         @Override
	         public void onDataAvailable() {
	            try {
	               do {
	            	   System.out.println("input.isRead: " + input.isReady());
	                  int length = input.read(buffer);
	                  sbuilder.append(new String(buffer, 0, length));
	               } while(input.isReady());
	            } catch (IOException ex) { ex.printStackTrace(); }
	         }
	         @Override
	         public void onAllDataRead() {
	            try {
	               acontext.getResponse().getWriter()
	                                     .write("...the response...");
	            } catch (IOException ex) { ex.printStackTrace(); }
	            acontext.complete();
	        	 
//	        	try {
//					resp.getWriter().write("...the response...");
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
	         }
	         @Override
	         public void onError(Throwable t) { t.printStackTrace(); }
	      });
		
		 resp.getWriter().write("...the end...");
		System.out.println("END");
	}
	
	
	
}
